import 'package:flutter/material.dart';
import '../services/ScreenAdapter.dart';
import "../services/SearchServices.dart";

class SearchPage extends StatefulWidget {
  const SearchPage({Key? key}) : super(key: key);

  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  var _keywords;
  List _historyListData = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this._getHistoryData();
  }
  // 初始化获取本地存储数据
  _getHistoryData() async {
    var _historyListData = await SearchServices.getHistoryList();
    setState(() {
      this._historyListData=_historyListData;
    });
  }

  // 长按删除弹窗
  _showAlertDialog(keywords) async{
    await showDialog(
      // 表示点击灰色背景的时候是否消失弹窗
      barrierDismissible: false,
        context: context,
        builder: (context){
        return AlertDialog(
          title: Text('提示信息'),
          content: Text('您确定要删除吗？'),
          actions: <Widget>[
            TextButton(
              child: Text('取消',style: TextStyle(color:Colors.blueAccent)),
              onPressed: (){
                Navigator.pop(context,'Cancle');
              },
            ),
            TextButton(
              child: Text('确定',style: TextStyle(color:Colors.blueAccent)),
              onPressed: () async{
               await SearchServices.removeHistoryData(keywords);
                this._getHistoryData();
                Navigator.pop(context,'ok');
              },
            ),
          ],
        );
        }
    );

  }

  Widget _historyListWidget() {

    if (this._historyListData.length > 0) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            child: Text('历史记录', style: Theme.of(context).textTheme.titleLarge),
          ),
          Divider(),
          Column(
              children: this._historyListData.map((value) {
            return Column(
              children: <Widget>[
                ListTile(
                  title: Text('${value}'),
                  onLongPress: (){
                    this._showAlertDialog("${value}");
                  },
                ),
                Divider(),
              ],
            );
          }).toList()),
          SizedBox(height: ScreenAdapter.height(100)),
         Row(
           mainAxisAlignment: MainAxisAlignment.center,
           children: <Widget>[
             InkWell(
               onTap: () {
                 SearchServices.clearHistoryList();
                 _getHistoryData();
               },
               child: Container(
                 width: ScreenAdapter.width(400),
                 height: ScreenAdapter.height(64),
                 decoration: BoxDecoration(
                     border: Border.all(
                         color: Color.fromRGBO(233, 233, 233, 1), width: 1)),
                 child: Row(
                   mainAxisAlignment: MainAxisAlignment.center,
                   children: <Widget>[Icon(Icons.delete), Text('清空历史记录')],
                 ),
               ),
             )
           ],
         )
        ],
      );
    } else {
      return Text('');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back,color: Colors.black87,),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        title: Container(
          child: TextField(
            autofocus: true,
            decoration: InputDecoration(
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(30),
                borderSide: BorderSide.none, //取消聚焦时候的下滑线
              ),
            ),
            onChanged: (value) {
              setState(() {
                this._keywords = value;
              });
            },
          ),
          height: ScreenAdapter.height(68),
          decoration: BoxDecoration(
            color: Color.fromRGBO(233, 233, 233, 0.8),
            borderRadius: BorderRadius.circular(30),
          ),
        ),
        actions: <Widget>[
          InkWell(
            child: Container(
              height: ScreenAdapter.height(68),
              width: ScreenAdapter.width(80),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Text('搜索',style: TextStyle(fontSize: ScreenAdapter.height(28),color: Colors.black87),)
                ],
              ),
            ),
            onTap: () {
              //本地存储历史记录
              SearchServices.setHistoryData(this._keywords);
              Navigator.pushReplacementNamed(context, '/productList',
                  arguments: {'keywords': this._keywords});
            },
          )
        ],
      ),
      body: Container(
        padding: EdgeInsets.all(ScreenAdapter.height(10)),
        child: ListView(
          children: <Widget>[
            Container(
              child: Text('热搜', style: Theme.of(context).textTheme.titleLarge),
            ),
            Divider(),
            Wrap(
              children: <Widget>[
                Container(
                  // padding: EdgeInsets.all(ScreenAdapter.height(10)),
                  padding: EdgeInsets.fromLTRB(
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10),
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10)),
                  margin: EdgeInsets.all(ScreenAdapter.height(10)),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(15)),
                  child: Text('手机'),
                ),
                Container(
                  // padding: EdgeInsets.all(ScreenAdapter.height(10)),
                  padding: EdgeInsets.fromLTRB(
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10),
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10)),
                  margin: EdgeInsets.all(ScreenAdapter.height(10)),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(15)),
                  child: Text('笔记本电脑'),
                ),
                Container(
                  // padding: EdgeInsets.all(ScreenAdapter.height(10)),
                  padding: EdgeInsets.fromLTRB(
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10),
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10)),
                  margin: EdgeInsets.all(ScreenAdapter.height(10)),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(15)),
                  child: Text('女装'),
                ),
                Container(
                  // padding: EdgeInsets.all(ScreenAdapter.height(10)),
                  padding: EdgeInsets.fromLTRB(
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10),
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10)),
                  margin: EdgeInsets.all(ScreenAdapter.height(10)),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(15)),
                  child: Text('耐克'),
                ),
                Container(
                  // padding: EdgeInsets.all(ScreenAdapter.height(10)),
                  padding: EdgeInsets.fromLTRB(
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10),
                      ScreenAdapter.height(20),
                      ScreenAdapter.height(10)),
                  margin: EdgeInsets.all(ScreenAdapter.height(10)),
                  decoration: BoxDecoration(
                      color: Color.fromRGBO(233, 233, 233, 0.9),
                      borderRadius: BorderRadius.circular(15)),
                  child: Text('衬衫'),
                ),

              ],
            ),
            SizedBox(
              height: 10.0,
            ),
            _historyListWidget()
          ],
        ),
      ),
    );
  }
}
